//
//  StorageManager.m
//  HItsBrige
//
//  Created by apple on 2019/3/28.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "StorageManager.h"
#define SYSTEM_DIR [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex : 0 ]

#define CORE_PLIST  [SYSTEM_DIR stringByAppendingPathComponent:CORE_PLIST_FILE_NAME]
#define CORE_PLIST_FILE_NAME   @"core.plist"
@implementation StorageManager
+(void) setProperty:(id)value forKey:(NSString *)key{
    
    @synchronized(self) {
        
        
        NSMutableDictionary * dic =  [[NSMutableDictionary alloc] initWithContentsOfFile:CORE_PLIST];
        
    
        if(!dic){
            dic=[[NSMutableDictionary alloc]init];
        }
        [dic setValue:value forKey:key];
        
        [dic writeToFile:CORE_PLIST atomically:YES];

    }
}
+(void) removeProperty:(NSString*)key {
    @synchronized(self) {
        
        
        
        NSMutableDictionary * cacheDic =  [[NSMutableDictionary alloc] initWithContentsOfFile:CORE_PLIST];
        
        if(cacheDic){
            [cacheDic removeObjectForKey:key];
            [cacheDic writeToFile:CORE_PLIST atomically:YES];
        }
        
    }
    
}
+(id) getProperty:(NSString*) key {
    @synchronized(self) {
        
        NSDictionary * dic =  [[NSDictionary alloc] initWithContentsOfFile:CORE_PLIST];
        

        if(dic){
            if ([dic objectForKey:key]) {
                if ([key isEqualToString:@"userInfo"]) {
                    UserInfoModel * model =[[UserInfoModel alloc]initWithDictionary:dic[@"userInfo"] error:nil];
                    return model;
                }
                return [dic objectForKey:key];
            }
        }
        return nil;
    }
}

+(void) clearAllProperties{
    @synchronized(self) {
        
        NSMutableDictionary * cacheDic =  [[NSMutableDictionary alloc] initWithContentsOfFile:CORE_PLIST];
        if(cacheDic){
            [cacheDic removeAllObjects];
            [cacheDic writeToFile:CORE_PLIST atomically:YES];
        }
        
    }
}

@end
